package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduCourseDTO;
import com.zenith.edu.po.vo.EduCourseVO;
import com.zenith.edu.service.EduCourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/course")
@Tag(name = "课程管理", description = "课程管理接口")
public class EduCourseController {
    
    @Autowired
    private EduCourseService eduCourseService;
    
    @SecurePostMapping(value = "/createCourse", roles = "ROLE_ADMIN", permission = "course:create")
    @Operation(summary = "创建课程")
    public Result<EduCourseVO> createCourse(@RequestBody EduCourseDTO dto) {
        EduCourseVO courseVO = eduCourseService.createCourse(dto);
        return Result.success(courseVO);
    }
    
    @SecurePostMapping(value = "/getCourseById/{id}", roles = "ROLE_ADMIN", permission = "course:query")
    @Operation(summary = "根据ID获取课程详情")
    public Result<EduCourseVO> getCourseById(@PathVariable Long id) {
        EduCourseVO courseVO = eduCourseService.getCourseById(id);
        if (courseVO == null) {
            return Result.fail("课程不存在");
        }
        return Result.success(courseVO);
    }
    
    @SecurePostMapping(value = "/updateCourse", roles = "ROLE_ADMIN", permission = "course:update")
    @Operation(summary = "更新课程信息")
    public Result<EduCourseVO> updateCourse(@RequestBody EduCourseDTO dto) {
        EduCourseVO courseVO = eduCourseService.updateCourse(dto);
        return Result.success(courseVO);
    }
    
    @SecurePostMapping(value = "/deleteCourse/{id}", roles = "ROLE_ADMIN", permission = "course:delete")
    @Operation(summary = "删除指定课程")
    public Result<Void> deleteCourse(@PathVariable Long id) {
        boolean result = eduCourseService.deleteCourse(id);
        if (!result) {
            return Result.fail("课程删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/getCourseList", roles = "ROLE_ADMIN", permission = "course:read")
    @Operation(summary = "获取课程列表")
    public Result<List<EduCourseVO>> getCourseList() {
        List<EduCourseVO> courseList = eduCourseService.getCourseList();
        return Result.success(courseList);
    }
}